﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;
using System.Text; 
namespace GpUI.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        #region 大图文
        public void ff()
        {
            HttpContent httpContent = new StringContent("");
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync("http://localhost:19451/api/Stand/PostShowStand", httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            var list = responseJson.ToString();
            var ss1 = list.Replace("[", "");
            var ss2 = ss1.Replace("{", "");
            var ss3 = ss2.Replace("}", "");
            var ss4 = ss3.Replace("]", "");
            List<string> listS = new List<string>();
            var list1 = ss4.Split(',');
            foreach (var item in list1)
            {
                listS.Add(item);
            }
            var getlist = listS;
            string tr = "";
            for (int i = 0; i < getlist.Count; i = i + 5)
            {
                var getid = getlist[i].ToString().Substring(getlist[i].IndexOf(":") + 1);
                var getRow1 = getlist[i + 1].ToString().Substring(getlist[i + 1].IndexOf(":") + 1);
                getRow1 = getRow1.Substring(2).ToString();
                getRow1 = getRow1.Substring(0, getRow1.Length - 2);

                var getRow2 = getlist[i + 2].ToString().Substring(getlist[i + 2].IndexOf(":") + 1);
                getRow2 = getRow2.Substring(2).ToString();
                getRow2 = getRow2.Substring(0, getRow2.Length - 2);

                var getRow3 = getlist[i + 3].ToString().Substring(getlist[i + 3].IndexOf(":") + 1);



                var getRow4 = getlist[i + 4].ToString().Substring(getlist[i + 4].IndexOf(":") + 1);
                getRow4 = getRow4.Substring(2).ToString();
                getRow4 = getRow4.Substring(0, getRow4.Length - 2);
                //tr += " <li><img typeof=\"foaf:Image\" src='"+getRow2+"' alt='Slide #3' title='Slide #3' /><div class='description'><span class='price'>"+getRow3+"</span><span class='name'>"+getRow1+"</span><a href='/Home/ImageTextLIst' class='shop'>去看看</a></div></li>";
                tr += "<li>";
                tr += "<img typeof=\"foaf:Image\" style=\"width:680px;height:523px;\" src='" + getRow2 + "' alt=\"Slide #3\" title=\"Slide #3\" /><div class=\"description\"><span class=\"price\">" + getRow3 + "</span><span class=\"name\">" + getRow1 + "</span><a href=\"/Home/ImageTextLIst?id="+getid+"\" class=\"shop\">去看看</a></div>";
                tr += "</li>";
            }     

            
            ViewBag.bb = tr;
        }
        #endregion

        public ActionResult Index()//主页
        {
            
            int id = Convert.ToInt32(Request["UserID"]);
            Session["UserID"] = id;
            Session["CustomerID"]=id;

            HttpContent httpContent = new StringContent("");
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync("http://localhost:19451/api/ToMenu/PostToMenushow", httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            var list = responseJson.ToString();
            var ss1 = list.Replace("[", "");
            var ss2 = ss1.Replace("{", "");
            var ss3 = ss2.Replace("}", "");
            var ss4 = ss3.Replace("]", "");
            List<string> listS = new List<string>();
            var list1 = ss4.Split(',');
            foreach (var item in list1)
            {
                listS.Add(item);
            }
            var getlist = listS;
            string tr = "";
            string td = "";
            bool state = false;
            for (int i = 0; i < getlist.Count; i = i + 5)
            {
                string getss = "";
                var getRowCount =getlist[i].ToString().Substring(getlist[i].IndexOf("\"PostReMenushow")+1);
                //
                //
                if (getRowCount.Length > 10) {
                    getss = getRowCount.ToString().Substring(0, getRowCount.IndexOf("w") + 1);
                }
                var getid = getlist[i].ToString().Substring(getlist[i].LastIndexOf(":") + 1);

                var getRow1 = getlist[i + 1].ToString().Substring(getlist[i + 1].IndexOf(":") + 1);
                getRow1 = getRow1.Substring(2).ToString();
                getRow1 = getRow1.Substring(0, getRow1.Length - 2);

                var getRow2 = getlist[i + 2].ToString().Substring(getlist[i + 2].IndexOf(":") + 1);
                getRow2 = getRow2.Substring(2).ToString();
                getRow2 = getRow2.Substring(0, getRow2.Length - 2);

                var getRow3 = getlist[i + 3].ToString().Substring(getlist[i + 3].IndexOf(":") + 1);
                getRow3 = getRow3.Substring(2).ToString();
                getRow3 = getRow3.Substring(0, getRow3.Length - 2);


                var getRow4 = getlist[i + 4].ToString().Substring(getlist[i + 4].IndexOf(":") + 1);

             
                if (getss == "PostReMenushow")
                {
                state=true;
                }
                if (state) {
                    td += "<li class='meal'>";
                    td += "<div class='img-holder'><img typeof='foaf:Image' src='" + getRow2 + "' width='191' height='148' alt='' /></div><h1><a href='/product/phasellus-ante-erat'>" + getRow1 + "</a></h1><p>" + getRow3 + "</p><span class='price'>$" + getRow4 + "</span><div class='add-to-cart'><form action='/' method='post' id='uc-product-add-to-cart-form-4--2' accept-charset='UTF-8'><div><input type='hidden' name='qty' value='1' /><input type='hidden' name='form_build_id' value='form---niTb3hToK3h3UDDW-OcFnVf0e8l7h8G6kkHtls1Uk' /><input type='hidden' name='form_id' value='uc_product_add_to_cart_form_4' /><div class='form-actions form-wrapper' id='edit-actions--5'><input class='node-add-to-cart form-submit' type='button' id='edit-submit-4' name='op' value='加入购物车' onclick='add(" + getid + ")' /></div></div></form></div>";
                    td += "</li>";
                }
                else
                {
                    tr += "  <li class='meal'>";
                    tr += "<div class='img-holder'><img typeof='foaf:Image' src='" + getRow2 + "' width='327' height='198' alt='' /></div><h1><a href='/product/phasellus-ante-erat'>" + getRow1 + "</a></h1><p>" + getRow3 + "</p><span class='price'>$" + getRow4 + "</span><div class='add-to-cart'><form action='/' method='post' id='uc-product-add-to-cart-form-4--2' accept-charset='UTF-8'><div><input type='hidden' name='qty' value='1' /><input type='hidden' name='form_build_id' value='form---niTb3hToK3h3UDDW-OcFnVf0e8l7h8G6kkHtls1Uk' /><input type='hidden' name='form_id' value='uc_product_add_to_cart_form_4' /><div class='form-actions form-wrapper' id='edit-actions--5'><input class='node-add-to-cart form-submit' type='button' id='edit-submit-4' name='op' value='加入购物车' onclick='add(" + getid + ")' /></div></div></form></div>";
                    tr += " </li>";
                }
            }
            ViewBag.s = tr;
            ViewBag.c = td;


            ff();
            return View();
        }
      // [App_Start.ShowQuan]
        public ActionResult Menu()//水果点心菜单
        {
            return View();
        }
       // [App_Start.ShowQuan]
        public ActionResult work()//工作餐菜单
        {
            return View();
        }
       // [App_Start.ShowQuan]
        public ActionResult drink()//饮品菜单
        {
            return View();
        }
       // [App_Start.ShowQuan]
         public ActionResult setmeal()//套餐菜单
         {
             return View();
         }
        //[App_Start.ShowQuan]
        public ActionResult sylefood()//西餐菜单
        {
            return View();
        }
        //[App_Start.ShowQuan]
        public ActionResult ChinaMenu()//中餐菜单
        {
            return View();
        }
       // [App_Start.ShowQuan]
        public ActionResult ShoppingCar()//购物车
        {
            return View();
        }
       // [App_Start.ShowQuan]
        public ActionResult ShoppingCarList()//购物车清单
        {
            return View();
        }
       //[App_Start.ShowQuan]
        public ActionResult ImageTextLIst(int id)//大图文列表
        {
            ViewBag.id = id;
            HttpContent httpContent = new StringContent("");
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync("http://localhost:19451/api/Stand/PostStand?id=" + id + "", httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            var list = responseJson.ToString();
            var ss1 = list.Replace("[", "");
            var ss2 = ss1.Replace("{", "");
            var ss3 = ss2.Replace("}", "");
            var ss4 = ss3.Replace("]", "");
            List<string> listS = new List<string>();
            var list1 = ss4.Split(',');
            foreach (var item in list1)
            {
                listS.Add(item);
            }
            var getlist = listS;
            StringBuilder str = new StringBuilder();
            for (int i = 0; i < getlist.Count; i = i + 5)
            {
                var getRow1 = getlist[i + 1].ToString().Substring(getlist[i + 1].IndexOf(":") + 1);
                getRow1 = getRow1.Substring(2).ToString();
                getRow1 = getRow1.Substring(0, getRow1.Length - 2);

                var getRow2 = getlist[i + 2].ToString().Substring(getlist[i + 2].IndexOf(":") + 1);
                getRow2 = getRow2.Substring(2).ToString();
                getRow2 = getRow2.Substring(0, getRow2.Length - 2);

                var getRow3 = getlist[i + 3].ToString().Substring(getlist[i + 3].IndexOf(":") + 1);

                var getRow4 = getlist[i + 4].ToString().Substring(getlist[i + 4].IndexOf(":") + 1);
                getRow4 = getRow4.Substring(2).ToString();
                getRow4 = getRow4.Substring(0, getRow4.Length - 2);

                str.Append("<div class='field field-name-field-blog-inage field-type-image field-label-hidden'><div class='field-items'><div class='field-item even'><a href='/blog/convallis-sit-amet-tellus'><img typeof='foaf:Image' src='" + getRow2 + "' width='650' height='300' alt='' /></a></div></div></div>        <h2 property='dc:title' datatype=''><a href='/blog/convallis-sit-amet-tellus'>" + getRow1 + "</a></h2>");
                str.Append("<div class='submitted'>");
                str.Append("<span property='dc:date dc:created' content='2012-09-13T17:55:15+07:00' datatype='xsd:dateTime' rel='sioc:has_creator'>Submitted by <span class='username' xml:lang='' about='/users/admin' typeof='sioc:UserAccount' property='foaf:name'>admin</span> " + DateTime.Now.ToString("yyyy MM dd hh:mm:ss") + "</span>");
                str.Append("</div>");
                str.Append("<div class='content'>");
                str.Append("<div class='field field-name-body field-type-text-with-summary field-label-hidden'><div class='field-items'><div class='field-item even' property='content:encoded'><p>" + getRow4 + "</p></div></div></div>");
                str.Append("</div>");
                str.ToString();
            }
            ViewBag.ff = str;
            return View();

     
        }
       // [App_Start.ShowQuan]
        public ActionResult ContactUs()//联系我们
        {
            return View();
        }
       // [App_Start.ShowQuan]
        public ActionResult PropertyPage()//内容页
        {
            return View();
        }
        //[App_Start.ShowQuan]
        public ActionResult Specification()//说明
        {
            return View();
        }
        //[App_Start.ShowQuan]
        public ActionResult quanMenu()//全部菜单
        {
            ViewBag.Menu = WaterfallMove(6);
            return View();
        }
        public string WaterfallMove(int size)
        {
            HttpContent httpContent = new StringContent(size.ToString());
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync("http://localhost:19451/api/Menu/WaterfallMove", httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            var list = responseJson.ToString();
            var count = 0;
            if (size == 6)
            {
                count = size;
            }
            else
            {
                count = 1;
            }

            if (count == 1)
            {
                return list;
            }
            else
            {
                var ss1 = list.Replace("[", "");
                var ss2 = ss1.Replace("{", "");
                var ss3 = ss2.Replace("}", "");
                var ss4 = ss3.Replace("]", "");
                List<string> listS = new List<string>();
                var list1 = ss4.Split(',');
                foreach (var item in list1)
                {
                    listS.Add(item);
                }
                var getlist = listS;
                string tr = "";
                var GetCount = 0;
                var PageCount = Convert.ToInt32(getlist[0].Substring(getlist[0].IndexOf(":") + 1).ToString());
                for (int i = 3; i < ((PageCount - count) * (Convert.ToInt32(getlist.Count) - 3)); i = i + 6)
                {
                    var SumCount = Convert.ToInt32(getlist[2].Substring(getlist[2].IndexOf(":") + 1));


                    if (SumCount > GetCount)
                    {
                        var getRow1 = getlist[i + 1].ToString().Substring(getlist[i + 1].IndexOf(":") + 1);//姓名
                        getRow1 = getRow1.Substring(1).ToString();
                        getRow1 = getRow1.Substring(0, getRow1.Length - 1);

                        var getRow2 = getlist[i + 2].ToString().Substring(getlist[i + 2].IndexOf(":") + 1);//路径
                        getRow2 = getRow2.Substring(1).ToString();
                        getRow2 = getRow2.Substring(0, getRow2.Length - 1);

                        var getRow3 = getlist[i + 3].ToString().Substring(getlist[i + 3].IndexOf(":") + 1);//介绍
                        getRow3 = getRow3.Substring(1).ToString();
                        getRow3 = getRow3.Substring(0, getRow3.Length - 1);

                        var getRow4 = getlist[i + 4].ToString().Substring(getlist[i + 4].IndexOf(":") + 1); //价格

                        var getRow5 = "";
                        if (getlist[i].ToString().Length > 10)
                        {
                            var getRow6 = getlist[i].ToString().Substring(getlist[i].IndexOf(":") + 1);//ID 
                            getRow5 = getRow6.Substring(getRow6.IndexOf(":") + 1);
                        }
                        else
                        {
                            getRow5 = getlist[i].ToString().Substring(getlist[i].IndexOf(":") + 1);//ID
                        }

                        if (GetCount % 2 != 0)
                        {
                            tr += "<div class=\"pin\" id='Pin" + GetCount + "'>";
                            tr += "<div class=\"box\">";
                            tr += "<div  class='right-part'><h4><span class='text'>" + getRow1 + "</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image' src='" + getRow2 + "' width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>" + getRow1 + "</a></h5><span>$" + getRow4 + "</span></div><div class='description'><p>" + getRow3 + "</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车'onclick='fun(" + getRow5 + ")' /></div></li></ul></div>";
                            tr += " </div>";
                            tr += "</div>";
                        }
                        else
                        {
                            tr += "<div class=\"pin\"  id='Pin" + GetCount + "'>";
                            tr += "<div class=\"box\">";
                            tr += "<div  class='left-part'><h4><span class='text'>" + getRow1 + "</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image' src='" + getRow2 + "' width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>" + getRow1 + "</a></h5><span>$" + getRow4 + "</span></div><div class='description'><p>" + getRow3 + "</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车'onclick='fun(" + getRow5 + ")' /></div></li></ul></div>";
                            tr += " </div>";
                            tr += "</div>";
                        }
                    }
                    else
                    {
                        if (GetCount % 2 != 0)
                        {
                            tr += "<div class=\"pin\"  id='Pin" + GetCount + "' hidden=\"hidden\">";
                            tr += "<div class=\"box\">";
                            tr += "<div  class='right-part'><h4><span class='text'>ss</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image'  width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>ss</a></h5><span>$s</span></div><div class='description'><p>ss</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车' /></div></li></ul></div>";
                            tr += " </div>";
                            tr += "</div>";
                        }
                        else
                        {
                            tr += "<div class=\"pin\"  id='Pin" + GetCount + "' hidden=\"hidden\">";
                            tr += "<div class=\"box\">";
                            tr += "<div  class='left-part'><h4><span class='text'>ss</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image'  width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>ss</a></h5><span>$s</span></div><div class='description'><p>ss</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车' /></div></li></ul></div>";
                            tr += " </div>";
                            tr += "</div>";
                        }
                    }
                    GetCount++;
                }
                return tr;
            }
        }


       // [App_Start.ShowQuan]
        public ActionResult Manager()
        {
            return View();
        }
        public ActionResult tcp()
        {
            int id = Convert.ToInt32(Request["UserID"]);
            Session["UserID"] = id;
            Session["CustomerID"] = id; ;
            return Content("<script>location.href = '/admin/show'</script>");
        }

        /// <summary>
        /// 菜单模糊查询
        /// </summary>
        /// <returns></returns>
        public ActionResult LikeMenu()
        {
            var ss = "";
            if (Request["GetName"] != null)
            {
                ss = Request["GetName"].ToString();
            }
            HttpContent httpContent = new StringContent(ss);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var httpClient = new HttpClient();
            var responseJson = httpClient.PostAsync("http://localhost:19451/api/Menu/PostLikeMenu", httpContent)
                .Result.Content.ReadAsStringAsync().Result;
            var list = responseJson.ToString();
            var ss1 = list.Replace("[", "");
            var ss2 = ss1.Replace("{", "");
            var ss3 = ss2.Replace("}", "");
            var ss4 = ss3.Replace("]", "");
            List<string> listS = new List<string>();
            var list1 = ss4.Split(',');
            foreach (var item in list1)
            {
                listS.Add(item);
            }
            var getlist = listS;
            string tr = "";
            for (int i = 0; i < getlist.Count; i = i + 6)
            {
                var getRow1 = getlist[i + 1].ToString().Substring(getlist[i + 1].IndexOf(":") + 1);//名称
                getRow1 = getRow1.Substring(2).ToString();
                getRow1 = getRow1.Substring(0, getRow1.Length - 2);

                var getRow2 = getlist[i + 2].ToString().Substring(getlist[i + 2].IndexOf(":") + 1);//路径
                getRow2 = getRow2.Substring(2).ToString();
                getRow2 = getRow2.Substring(0, getRow2.Length - 2);

                var getRow4 = getlist[i + 4].ToString().Substring(getlist[i + 4].IndexOf(":") + 1);//价格

                var getRow3 = getlist[i].ToString().Substring(getlist[i].IndexOf(":") + 1);//ID

                var getRow5 = getlist[i + 3].ToString().Substring(getlist[i + 3].IndexOf(":") + 1);//介绍
                getRow5 = getRow5.Substring(2).ToString();
                getRow5 = getRow5.Substring(0, getRow5.Length - 2);

                if (Convert.ToInt32(getRow3) % 2 == 0)
                {
                    tr += "<div class=\"pin\">";
                    tr += "<div class=\"box\">";
                    tr += "<div  class='right-part'><h4><span class='text'>" + getRow1 + "</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image' src='" + getRow2 + "' width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>" + getRow1 + "</a></h5><span>$" + getRow4 + "</span></div><div class='description'><p>" + getRow5 + "</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车'onclick='fun(" + getRow3 + ")' /></div></li></ul></div>";
                    tr += " </div>";
                    tr += "</div>";
                }
                else
                {
                    tr += "<div class=\"pin\">";
                    tr += "<div class=\"box\">";
                    tr += "<div  class='left-part' ><h4><span class='text'>" + getRow1 + "</span><span class='line'></span></h4><ul><li><div class='img-holder'><div class='canvas'></div><img typeof='foaf:Image' src='" + getRow2 + "' width='108' height'90' alt='' /></div><div class='price'><h5><a href='/product/convallis-sit-amet-tellus'>" + getRow1 + "</a></h5><span>$" + getRow4 + "</span></div><div class='description'><p>" + getRow5 + "</p></div><div class='form-actions form-wrapper' id='edit-actions--2'><input class='node-add-to-cart form-submit' type='submit' id='edit-submit-3' name='op' value='加入购物车'onclick='fun(" + getRow3 + ")' /></div></li></ul></div>";
                    tr += " </div>";
                    tr += "</div>";
                }
            }
            ViewBag.LikeMenu = tr;
            return View();
        }


    }
}